#include <Wire.h>

#include <MPU6050.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

MPU6050 mpu;

#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

#define OLED_RESET     -1

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Threshold values for each axis (adjust as needed)

const int16_t thresholdX = 1000; // Example threshold for x-axis

const int16_t thresholdY = 1000; // Example threshold for y-axis

const int16_t thresholdZ = 1000; // Example threshold for z-axis

boolean rescued = false; // Flag to indicate if the person has been rescued

void setup() {

  Serial.begin(9600);

  Wire.begin();

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {

    Serial.println(F("SSD1306 allocation failed"));

    for (;;);

  }

  delay(2000);

  display.clearDisplay();

  display.setTextColor(SSD1306_WHITE);

  display.setTextSize(1);

  display.setCursor((SCREEN_WIDTH - 12 * 6) / 2, 0);

  display.println("Water Alarm and Rescue Bracelet");

  display.setCursor((SCREEN_WIDTH - 11 * 6) / 2, 20);

  display.println("Normal Status...");

  display.display();

  // Initialize MPU6050

  mpu.initialize();

  mpu.setDLPFMode(0); // Disable DLPF for faster response

}

void loop() {

  int16_t ax, ay, az;

  mpu.getAcceleration(&ax, &ay, &az);

  // Print sensor data if the person is not rescued

  if (!rescued) {

    Serial.print("AccelX:");

    Serial.print(ax);

    Serial.print(", AccelY:");

    Serial.print(ay);

    Serial.print(", AccelZ:");

    Serial.println(az);

  }

  // Check if the sensor readings indicate a person has fallen into water

  if (ax < thresholdX && ay < thresholdY && az < thresholdZ) {

    display.clearDisplay();

    display.setTextColor(SSD1306_WHITE);

    display.setTextSize(1);

    display.setCursor(0, 0);

    display.println("Help Alarm");

    display.display();



    // Print rescue information and update rescued flag

    Serial.println("Urgent: Person has fallen into water! Rescue needed!");

    Serial.print("Location: ");

    // Print location information here

    rescued = true; // Set rescued flag to true

  } else {

    // If the person is rescued, print rescue message

    if (rescued) {

      display.clearDisplay();

      display.setTextColor(SSD1306_WHITE);

      display.setTextSize(1);

      display.setCursor(0, 0);

      display.println("Rescue Completed");

      display.display();



      Serial.println("Person has been rescued!");

      // Print location information here

      rescued = false; // Reset rescued flag

    }

  }

  // Delay for 2 seconds between normal printing

//  delay(2000);

  // Clear OLED display and show normal status

  display.clearDisplay();

  display.setTextColor(SSD1306_WHITE);

  display.setTextSize(1);

  display.setCursor((SCREEN_WIDTH - 12 * 6) / 2, 0);

  display.println("Water Alarm and Rescue Bracelet");

  display.setCursor((SCREEN_WIDTH - 11 * 6) / 2, 20);

  display.println("Normal Status...");

  display.display();

}
